package org.vt.ece4564.pcw.textinclass;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class PostContactsRequest extends AsyncTask<Void, Void, Void> {

	private List<String> names_;
	private List<String> numbers_;
	private Cookie cookie_;
	private PostContactsActivity postContactsActivity_;
	
	private final String URL = "http://pcw-textinclass.appspot.com/postContacts";
	
	private String responseString_ = "";
	private boolean success_ = false;
	
	public PostContactsRequest(List<String> names, List<String> numbers, Cookie cookie, 
			PostContactsActivity postContactsActivity) {
		
		names_ = names;
		numbers_ = numbers;
		cookie_ = cookie;
		postContactsActivity_ = postContactsActivity;
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(URL);
			
			post.addHeader("Cookie", cookie_.getName() + "=" + cookie_.getValue());
			
			List<NameValuePair> data = new ArrayList<NameValuePair>();
			
			for (int i = 0; i < names_.size(); i++) {
				data.add(new BasicNameValuePair("names", names_.get(i)));
				data.add(new BasicNameValuePair("numbers", numbers_.get(i)));
			}
			
			HttpEntity entity = new UrlEncodedFormEntity(data);
			
			post.setEntity(entity);
			
			HttpResponse response = client.execute(post);
			
			if (response.getStatusLine().getStatusCode() != 200) {
				Log.d("PostContactsRequestError", response.getStatusLine().toString());
				return null;
			}
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(
											response.getEntity().getContent()));
			
			String line;
			
			while ((line = reader.readLine()) != null) {
				responseString_ += line;
			}
			
			if (responseString_.equals("Success")) {
				success_ = true;
			}
			
		} catch (Exception e) {
			Log.d("PostContactsRequestException", e.toString());
		}
		
		return null;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		if (!success_) {
			postContactsActivity_.resetScreen();
			
			Toast toast = Toast.makeText(postContactsActivity_.getApplicationContext(),
											responseString_, Toast.LENGTH_SHORT);
			toast.show();
		}
		else {
			Intent intent = new Intent(postContactsActivity_, LoggedInActivity.class);
			intent.putExtra("cookieName", cookie_.getName());
			intent.putExtra("cookieValue", cookie_.getValue());
			
			postContactsActivity_.startActivity(intent);
		}
	}

}
